package 并发编程08.原子操作类;

import 并发编程08.原子操作类.model.Goods;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author idea
 * @data 2019/1/24
 */
public class AtomicDemo3 implements Runnable {


    static AtomicIntegerFieldUpdater<Goods> goods2 = AtomicIntegerFieldUpdater.newUpdater(Goods.class, "counts");

    static Goods goods = new Goods("idea", 3);

    public static void main(String[] args) {


        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(new AtomicDemo3());
            threadList.add(thread);
        }
        for (Thread thread : threadList) {
            thread.start();
        }
    }

    @Override
    public void run() {
        int count = goods2.getAndDecrement(goods);
        if (count < 0) {
            System.out.println("已经抢完了");
        }

    }
}
